<template>
  <div class="lingquan" v-if="module.modules.length && list.length">
    <div class="container-coupon">
      <div class="names flex-x">
        <div class="list-name">{{  (module.modules[0].title) }}</div>
        <div class="list-desc">{{ module.modules[0].subTitle }}</div>
      </div>

      <div class="detail-page" v-if="module.modules[0].pic">
        <img
          class="detail-page-img"
          style="height: 120px"
          :src="fileBaseUrl + module.modules[0].pic"
        />
      </div>

      <div class="coupon-list flex-x">
        <div
          class="coupon-item"
          v-for="item in list"
          :key="item.detail.availableTime.couponId"
        >
          <img
            class="coupon-bg"
            :src="require('@/assets/icon/pic_quanbac.png')"
            style="width: 111px; height: 120px"
          />
          <div class="coupon-content flex-y-center">
            <div
              class="coupon-name flex-x"
              v-if="item.detail.prototype.couponType === 'exchange'"
            >
              <div>{{ item.detail.prototype.denomination / 100 }}</div>
              <div class="unit">元</div>
            </div>
            <div
              class="coupon-name flex-x"
              v-else-if="
                item.detail.prototype.couponType === 'discount' ||
                item.detail.prototype.couponType === 'discount_wx_busi'
              "
            >
              <div>
                {{ myFied(item.detail.prototype.discountPercent) }}
              </div>
              <div class="unit">折</div>
            </div>
            <div class="coupon-name flex-x" v-else>
              <div>{{ item.detail.prototype.denomination / 100 }}</div>
              <div class="unit">元</div>
            </div>
            <div
              class="threshold"
              v-if="item.detail.prototype.transactionMinimum / 100 == 0"
            >
              无门槛
            </div>
            <div class="threshold" v-else>
              满{{ item.detail.prototype.transactionMinimum / 100 }}可用
            </div>
            <button class="receive flex-x-center-center">立即领取</button>
          </div>
        </div>
      </div>
    </div>
  </div>
  <ModuleNotConfig :module="module" v-else />
</template>

<script>
import { getCardCouponByIds } from "@/views/cardsAndOffer/api/coupon";
export default {
  props: {
    module: {
      type: Object,
    },
    fileBaseUrl: {
      type: String,
    },
  },
  data() {
    return {
      list: [],
    };
  },
  watch: {
    module: {
      handler() {
        if (!this.module.modules || !this.module.modules[0] || !this.module.modules[0].biz) return;
        const ids = this.module.modules[0].biz.params.couponIds;

        if (ids?.length) {
          this.getCardCouponByIds(ids);
        } else {
          this.list = [];
        }
      },
      deep: true,
      immediate: true,
    },
  },
  mounted() {
    if (!this.module.modules || !this.module.modules[0] || !this.module.modules[0].biz) return;
    const ids = this.module.modules[0].biz.params.couponIds;
    if (ids?.length) {
      this.getCardCouponByIds(ids);
    } else {
      this.list = [];
    }
  },
  methods: {
    getCardCouponByIds(ids) {
      getCardCouponByIds({ prototypeId: ids }).then((res) => {
        this.list = res.data;
      });
    },
    myFied(discountPercent) {
      var num = (discountPercent / 10).toFixed(1);
      return num;
    },
  },
};
</script>

<style lang="scss" scoped>
.container-coupon {
  padding: 0px 9px;
  padding-bottom: 5px;
  .unit {
    margin-left: 1px;
    font-size: 20px;
    line-height: 28px;
    font-family: PingFang SC;
    font-weight: bold;
    color: #e74b4b;
  }
  .names {
    font-family: PingFang SC;
    align-items: baseline;
    line-height: normal;
    .list-name {
      font-size: 16px;
      font-weight: bold;
      color: #000000;
    }
    .list-desc {
      font-size: 13px;
      font-weight: 400;
      color: #888888;
      margin-left: 6px;
    }
    margin-bottom: 14px;
  }
  .detail-page {
    overflow: hidden;
    .detail-page-img {
      border-radius: 10px;
    }
    margin-bottom: 16px;
  }
  .coupon-list {
    font-family: PingFang SC;
    flex-wrap: wrap;
    .coupon-item {
      position: relative;
      height: 120px;
      width: 110px;
      margin-right: 3.5px;
      margin-bottom: 5px;
    }
    .coupon-bg {
      position: absolute;
      z-index: 1;
    }
    .coupon-content {
      position: relative;
      z-index: 2;
      padding: 13px 0px 9px;
      .coupon-name {
        font-size: 24px;
        line-height: 27px;
        align-items: baseline;
        font-family: Source Han Sans SC;
        font-weight: bold;
        color: #e74b4b;
        align-items: flex-end;
      }
      .threshold {
        font-size: 12px;
        font-weight: 400;
        color: #353535;
        margin: 10px 0px 24px;
      }
      .receive {
        width: 86px;
        height: 24px;
        background: #e74b4b;
        border-radius: 12px;
        font-size: 12px;
        font-weight: bold;
        color: #ffffff;
      }
      .nowuse {
        border: 1px solid #e74b4b;
        background-color: #ffffff;
        color: #e74b4b;
      }
    }
  }
}
</style>